package com.cg.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.entity.AppVersion;
import com.cg.service.impl.AppVersionServiceImpl;
import com.cg.utils.DateUtils;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.UploadUtil;
import com.cg.utils.annotation.SysLog;

@RestController
@RequestMapping("/app/version")
public class AppVersionController{
	
	@Autowired
	AppVersionServiceImpl appVersionServiceImpl;
	
	@Autowired
	private UploadUtil uploadUtil;
	/**访问地址*/
	@Value("${global.upload.path}")
	private String path;
	/**存放地址*/
	@Value("${global.upload.docBase}")
	private String docBase;
	
	private String mypath="apkfile/";
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("app:version:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		
		List<AppVersion> versionList = appVersionServiceImpl.queryList(query);
		int total = appVersionServiceImpl.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", versionList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("app:version:info")
	public R info(@PathVariable("id") Long id){
		AppVersion version = appVersionServiceImpl.queryObject(id);
		
		
		return R.ok().put("appversion", version);
	}
	
	
	/**
	 * 业务列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("app:version:select")
	public R select(String type){
//		BusinessDoc doc=new BusinessDoc();
		
		List list = null;
		Map<String, Object> doc = new HashMap<>();
		list = appVersionServiceImpl.queryList(doc );
		 
		
		return R.ok().put("list", list);
	}
	
	/**
	 * 保存版本
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("app:version:save")
	public R save(@RequestBody AppVersion version){
		version.setCreateTime(DateUtils.dateToDateString(new Date()));
		appVersionServiceImpl.save(version);
		
		return R.ok();
	}
	
	/**
	 * 保存版本
	 */
	@SysLog("保存")
	@RequestMapping("/save2")
	@RequiresPermissions("app:version:save")
	public R save2( HttpServletRequest request){
		AppVersion version = new AppVersion ();
		version.setCreateTime(DateUtils.dateToDateString(new Date()));
		String filePath = uploadUtil.uploadImg(request,mypath);
		version.setUrl(filePath);
		appVersionServiceImpl.save(version);
		
		return R.ok();
	}
	/**
	 * 修改版本
	 */
	@SysLog("修改")
	@RequestMapping("/update2")
	@RequiresPermissions("app:version:update")
	public R update2(HttpServletRequest request,AppVersion version ){
//		version.setCreateTime(DateUtils.dateToDateString(new Date()));
		String filePath = uploadUtil.uploadImg(request,mypath);
		version.setUrl(filePath);
//		appVersionServiceImpl.update(version);
		
		return R.ok(filePath);
	}
	
	
	/**
	 * 修改版本
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("app:version:update")
	public R update(@RequestBody AppVersion version){
		
//		comment.set(getUserId());
		appVersionServiceImpl.update(version);
		
		return R.ok();
	}
	
	/**
	 * 删除版本
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("app:version:delete")
	public R delete(@RequestBody Long[] ids){
		
		appVersionServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
	
}
